package my.sample.bytebuffer;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class PositionsTry {

	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
/*		
		ByteBuffer intBuffer = ByteBuffer.allocate(8);
		intBuffer.putInt(4);
		intBuffer.putInt(1000);
		
		System.out.println(intBuffer);
		intBuffer.flip();
		
		System.out.println(new String(intBuffer.array(),  "ASCII"));
		*/
		
		ByteBuffer temp = ByteBuffer.allocate(1000);
		String name = "murali venugopal";
		
		temp = ByteBuffer.wrap(name.getBytes());
		printDetails(temp, "initial allocate");
		
		System.out.println(new String(temp.array(),Charset.forName("UTF-8")));
		
		
		
		
		System.exit(1);
		
		ByteBuffer nameBuffer = temp;
		printDetails(nameBuffer, "after allocate");
		
		byte[] first = new byte[6];
		
		printDetails(nameBuffer, "before reading 6 bytes");
//		nameBuffer.get(first, nameBuffer.position(), first.length);
		nameBuffer.get(first);
		printDetails(nameBuffer, "after reading 6 bytes");
		
		System.out.println(new String(first).toCharArray());
		
		nameBuffer.position(nameBuffer.position() + 1);
		
		byte[] second = new byte[9];
		
		printDetails(nameBuffer, "before reading 9 bytes");
//		nameBuffer.get(second, nameBuffer.position(), second.length);
		nameBuffer.get(second);
		printDetails(nameBuffer, "after reading 9 bytes");
		
		System.out.println(new String(second).toCharArray());
		
		
		System.exit(1);
		System.out.println(nameBuffer.getInt());
		nameBuffer.mark();
//		printDetails(nameBuffer);
		nameBuffer.reset();
//		printDetails(nameBuffer);
		
		
		
	}
	
	public static void printDetails(ByteBuffer buffer, String message) {
		System.out.println(message + " : capacity : " + buffer.capacity() + " position : " + buffer.position() + " limit : " + buffer.limit() + " remaining : "+ buffer.remaining() );
	}

}
